package uestc.lj.gulishop.order.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import uestc.lj.common.to.SeckillOrderTO;
import uestc.lj.gulishop.order.service.OrderService;

import java.io.IOException;

/**
 * 监听秒杀活动下单队列
 *
 * @Author:Crazlee
 * @Date:2021/6/24
 */
@Slf4j
@Component
@RabbitListener(queues = "order.seckill.order.queue")
public class OrderSeckillListener {
    @Autowired
    private OrderService orderService;


    /**
     * 监听秒杀单队列的信息
     *
     * @param seckillOrderTO
     * @param channel
     * @param message
     */
    @RabbitHandler
    public void listener(SeckillOrderTO seckillOrderTO, Channel channel, Message message) throws IOException {
        try {
            log.info("#######准备创建秒杀单的信息########");
            orderService.createSeckillOrder(seckillOrderTO);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
